package com.coffee.admin.service.supplier.impl;

import com.coffee.admin.api.supplier.query.SupplierBaseDeployQuery;
import com.coffee.admin.api.supplier.result.SupplierBaseDeployResult;
import com.coffee.admin.api.supplier.service.SupplierBaseDeployService;
import com.coffee.admin.service.supplier.bean.SupplierBaseDeploy;
import com.coffee.admin.service.supplier.dao.SupplierBaseDeployDao;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import com.coffee.sku.api.sku.query.SkuCategoryQuery;
import com.coffee.sku.api.sku.result.SkuCategoryResult;
import com.coffee.sku.service.sku.bean.SkuCategory;
import org.apache.commons.collections.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


@DubboService(dynamic = true)
public class SupplierBaseDeployServiceImpl implements SupplierBaseDeployService {
    @Resource
    private SupplierBaseDeployDao supplierBaseDeployDao;

    @Override
    public Page<SupplierBaseDeployResult> getPager(SupplierBaseDeployQuery param, PagerInfo pr) {
        Page<SupplierBaseDeployResult> pager = new Page<SupplierBaseDeployResult>(pr.getStart(), pr.getPageSize());
        Integer count = supplierBaseDeployDao.count(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<SupplierBaseDeploy> list = supplierBaseDeployDao.page(param, pr.getStart(), pr.getPageSize());
            pager.setData(DTOUtils.convertList(list, SupplierBaseDeployResult.class));
        }
        return pager;
    }

    @Override
    public Integer count(SupplierBaseDeployQuery param) {
        Integer count = supplierBaseDeployDao.count(param);
        return count;
    }

    @Override
    public SupplierBaseDeployResult getDetail(Integer id) {
        SupplierBaseDeploy item = supplierBaseDeployDao.findById(id);
        if (item == null) {
            return null;
        }
        SupplierBaseDeployResult result = new SupplierBaseDeployResult();
        BeanUtilExt.copyProperties(result, item);

        return result;
    }

    @Override
    public List<SupplierBaseDeployResult> getList(SupplierBaseDeployQuery param, PagerInfo pr) {
        List<SupplierBaseDeploy> list = supplierBaseDeployDao.page(param, pr.getStart(), pr.getPageSize());
        return DTOUtils.convertList(list, SupplierBaseDeployResult.class);
    }

    @Override
    public List<SupplierBaseDeployResult> getList(SupplierBaseDeployQuery param) {
        List<SupplierBaseDeploy> supplierBaseDeploys = supplierBaseDeployDao.list(param);
        Map<Integer, List<SupplierBaseDeploy>> integerListMap = supplierBaseDeploys.stream().collect(Collectors.groupingBy(SupplierBaseDeploy::getPid));
        List<SupplierBaseDeploy> supplierBaseDeployList = integerListMap.get(0);
        List<SupplierBaseDeployResult> supplierBaseDeployResults = DTOUtils.convertList(supplierBaseDeployList, SupplierBaseDeployResult.class);
        recursionSupplierBaseDeployResult(integerListMap, supplierBaseDeployResults);
        return supplierBaseDeployResults;
    }

    public void recursionSupplierBaseDeployResult(Map<Integer, List<SupplierBaseDeploy>> categoryMap, List<SupplierBaseDeployResult> skuCategoryResults) {
        for (SupplierBaseDeployResult supplierBaseDeployResult : skuCategoryResults) {
            Integer id = supplierBaseDeployResult.getId();
            List<SupplierBaseDeploy> supplierBaseDeploys = categoryMap.get(id);
            if (CollectionUtils.isNotEmpty(supplierBaseDeploys)) {
                List<SupplierBaseDeployResult> submenuList = DTOUtils.convertList(supplierBaseDeploys, SupplierBaseDeployResult.class);
                recursionSupplierBaseDeployResult(categoryMap, submenuList);
                supplierBaseDeployResult.setChildList(submenuList);
            }
        }
    }

    @Override
    public List<SupplierBaseDeployResult> getList() {
        SupplierBaseDeployQuery param = new SupplierBaseDeployQuery();
        List<SupplierBaseDeploy> list = supplierBaseDeployDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, SupplierBaseDeployResult.class);
    }


    @Override
    public void delete(Integer id) {
        supplierBaseDeployDao.deleteById(id);
    }

    @Override
    public void addOrUpdate(SupplierBaseDeployResult result) throws BusinessException {
        SupplierBaseDeploy item = new SupplierBaseDeploy();
        BeanUtilExt.copyProperties(item, result);
        if (item.getId() == null) {
            supplierBaseDeployDao.insert(item);
        } else {
            SupplierBaseDeploy tmp = supplierBaseDeployDao.findById(item.getId());
            if (tmp == null) {
                throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
            }
            supplierBaseDeployDao.updateById(item);
        }
    }

    @Override
    public Map<Integer, SupplierBaseDeployResult> getItemMap(Set<Integer> idSet) {
        Map<Integer, SupplierBaseDeployResult> map = new HashMap<Integer, SupplierBaseDeployResult>();
        List<SupplierBaseDeploy> list = supplierBaseDeployDao.findByIds(StringUtil.setToList(idSet));
        List<SupplierBaseDeployResult> resultList = DTOUtils.convertList(list, SupplierBaseDeployResult.class);
        for (SupplierBaseDeployResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Map<Integer, SupplierBaseDeployResult> getItemMap() {
        Map<Integer, SupplierBaseDeployResult> map = new HashMap<Integer, SupplierBaseDeployResult>();
        SupplierBaseDeployQuery param = new SupplierBaseDeployQuery();
        List<SupplierBaseDeploy> list = supplierBaseDeployDao.page(param, 0, Integer.MAX_VALUE);
        List<SupplierBaseDeployResult> resultList = DTOUtils.convertList(list, SupplierBaseDeployResult.class);
        for (SupplierBaseDeployResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }


}
